package com.egao.orderB.service.impl;

import com.egao.common.core.damon.results.PageResult2;
import com.egao.common.core.web.JsonResult;
import com.egao.common.system.entity.User;
import com.egao.orderB.biz.BillFeeBiz;
import com.egao.orderB.biz.BillFeeWriteOffBiz;
import com.egao.orderB.biz.BillFeeWriteOffItemBiz;
import com.egao.orderB.entity.BillFeeWriteOff;
import com.egao.orderB.entity.BillFeeWriteOffItem;
import com.egao.orderB.service.BillFeeWriteOffService;
import com.egao.orderB.service.SequenceService;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

//import com.egao.orderB.mapper.BillFeeWriteOffMapper;
//import javax.transaction.Transactional;

/**
 * 销账列表服务实现类
 * @author : Doman
 * @date : 2021-04-19 17:01:51
 * @version ：1.0
 */
@Service
public class BillFeeWriteOffServiceImpl /*extends ServiceImpl<BillFeeWriteOffMapper, BillFeeWriteOff> */implements BillFeeWriteOffService {

	@Autowired
	private BillFeeBiz billFeeBiz;
	@Autowired
	private BillFeeWriteOffBiz billFeeWriteOffBiz;
	@Autowired
	private BillFeeWriteOffItemBiz billFeeWriteOffItemBiz;
	@Autowired
	private SequenceService sequenceService;

    /**
    @Override
    public PageResult<BillFeeWriteOff> listPage(PageParam<BillFeeWriteOff> page) {
        List<BillFeeWriteOff> records = baseMapper.listPage(page);
        return new PageResult<>(records, page.getTotal());
    }

    @Override
    public List<BillFeeWriteOff> listAll(Map<String, Object> page) {
        return baseMapper.listAll(page);
    } */

    @Override
    public JsonResult list(PageResult2<T> page, BillFeeWriteOff billFeeWriteOff, User loginUser)  throws Exception{
        return billFeeWriteOffBiz.list(page,billFeeWriteOff,loginUser);
    }

    @Override
    public JsonResult list(BillFeeWriteOff billFeeWriteOff, User loginUser) {
        return JsonResult.ok().setData(billFeeWriteOffBiz.list(billFeeWriteOff,loginUser));
    }

    @Override
    public JsonResult getById(BillFeeWriteOff billFeeWriteOff) {
        return JsonResult.ok().setData(billFeeWriteOffBiz.getById(billFeeWriteOff));
    }

    @Transactional
    @Override
    public JsonResult save(BillFeeWriteOff billFeeWriteOff, User loginUser) {
        billFeeWriteOff.setWriteOffCode(sequenceService.genCertCode("sg", "1"));
        int id = billFeeWriteOffBiz.save(billFeeWriteOff, loginUser);
        for (BillFeeWriteOffItem billFeeWriteOffItem: billFeeWriteOff.getBillFeeWriteOffItemList()){
            billFeeWriteOffItem.setWriteOffId(id+"");
            billFeeWriteOffItemBiz.save(billFeeWriteOffItem,loginUser);
            billFeeBiz.updateWriteOffAccounts(billFeeWriteOffItem,loginUser);
        }
        return JsonResult.ok("添加成功");
    }

    @Transactional
    @Override
    public JsonResult update(BillFeeWriteOff billFeeWriteOff, User loginUser) {
        if (billFeeWriteOffBiz.update(billFeeWriteOff, loginUser)) {
            return JsonResult.ok("修改成功");
        }
        return JsonResult.error("修改失败");
    }

    @Transactional
    @Override
    public JsonResult remove(BillFeeWriteOff billFeeWriteOff, User loginUser) {
        if (billFeeWriteOffBiz.remove(billFeeWriteOff, loginUser)) {
            return JsonResult.ok("删除成功");
        }
        return JsonResult.error("删除失败");
    }

}
